package com.xnccc.fragment;

import com.owen.mapx.MapX;
import com.xnccc.audioset.MainActivity;
import com.xnccc.audioset.R;
import com.xnccc.view.VerticalSeekBar;
import com.xnccc.view.VerticalSeekBar.OnSeekBarChangeListener;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import util.DP;

public class BaseEqFragment extends Fragment implements OnSeekBarChangeListener {

	private MapX bird;
	int show[] = { R.id.txtshow1, R.id.txtshow2, R.id.txtshow3, R.id.txtshow4, R.id.txtshow5, R.id.txtshow6,
			R.id.txtshow7, R.id.txtshow8 };
	int bars[] = { R.id.seekbar1, R.id.seekbar2, R.id.seekbar3, R.id.seekbar4, R.id.seekbar5, R.id.seekbar6,
			R.id.seekbar7, R.id.seekbar8 };
	TextView[] showTxts = new TextView[8];
	VerticalSeekBar seekBars[] = new VerticalSeekBar[8];
	private CheckBox noiseSwitch;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		bird = MainActivity.bird;
		View mView = inflater.inflate(R.layout.fragment_base_eq, container, false);
		initView(mView);
		return mView;
	}

	private void initView(final View mView) {

		((TextView) mView.findViewById(R.id.txtBar1)).setText(DP.LFE_UP + "HZ");
		((TextView) mView.findViewById(R.id.txtBar8)).setText(DP.LFE_DOWN + "HZ");

		noiseSwitch = (CheckBox) mView.findViewById(R.id.noise_switch);
		noiseSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (arg1) {
					bird.switchSRC(MapX.SRC_NOISE);
					bird.setNoiseChannel(MapX.CH_LFE);
				} else {
					bird.switchSRC(MapX.SRC_ARM);
					bird.setNoiseChannel(MapX.CH_NULL);
				}
			}
		});
		mView.post(new Runnable() {

			@Override
			public void run() {
				for (int i = 0; i < bars.length; i++) {
					int nowpro = bird.getPEQ(MapX.CH_LFE, (i + 1));
					showTxts[i] = (TextView) mView.findViewById(show[i]);
					seekBars[i] = (VerticalSeekBar) mView.findViewById(bars[i]);
					seekBars[i].setOnSeekBarChangeListener(BaseEqFragment.this);
					showTxts[i].setText(nowpro + "");
					seekBars[i].setProgress(30 - nowpro);
				}
			}
		});
	}

	private void chanage(int num, int vol) {
		showTxts[num].setText(vol + "");
		bird.setPEQ(MapX.CH_LFE, (num + 1), vol);
	}

	@Override
	public void onProgressChanged(VerticalSeekBar VerticalBar, int progress, boolean fromUser) {

		if (fromUser) {
			Log.e("niw", progress + "");
			int nowindex = 30 - progress;
			switch (VerticalBar.getId()) {
			case R.id.seekbar1:
				chanage(0, nowindex);
				break;
			case R.id.seekbar2:
				chanage(1, nowindex);
				break;
			case R.id.seekbar3:
				chanage(2, nowindex);
				break;
			case R.id.seekbar4:
				chanage(3, nowindex);
				break;
			case R.id.seekbar5:
				chanage(4, nowindex);
				break;
			case R.id.seekbar6:
				chanage(5, nowindex);
				break;
			case R.id.seekbar7:
				chanage(6, nowindex);
				break;
			case R.id.seekbar8:
				chanage(7, nowindex);
				break;
			}
		}
	}

	@Override
	public void onStartTrackingTouch(VerticalSeekBar VerticalBar) {

	}

	@Override
	public void onStopTrackingTouch(VerticalSeekBar VerticalBar) {

	}
}
